package cn.sc.summer.construction.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.sc.summer.constant.project.DateConstant;
import cn.sc.summer.constant.sms.SmsConstant;
import cn.sc.summer.constant.token.TokenConstant;
import cn.sc.summer.constant.util.ConvertUtil;
import cn.sc.summer.constant.util.DateUtilX;
import cn.sc.summer.daoapi.dao.SmsTemplateMapper;
import cn.sc.summer.daoapi.dao.SysUserInfoMapper;
import cn.sc.summer.daoapi.po.SmsTemplate;
import cn.sc.summer.domain.service.RunnerService;
import cn.sc.summer.exception.exceptions.CommonException;
import cn.sc.summer.redis.util.RedisHelper;
import cn.sc.summer.sms.enums.SmsServerEnum;
import cn.sc.summer.sms.properties.SmsProperties;
import cn.sc.summer.token.po.SysUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 类名：启动后执行业务
 *
 * @author a-xin
 * @date 2024/1/30 14:26
 */
@Slf4j
@Service
public class RunnerServiceImpl implements RunnerService {

    @Resource
    private SysUserInfoMapper sysUserInfoMapper;

    @Resource
    private SmsTemplateMapper smsTemplateMapper;

    @Resource
    private SmsProperties smsProperties;

    /**
     * 缓存用户信息
     */
    @Override
    public void cacheUserInfo() {
        List<SysUserInfo> sysUserInfos = sysUserInfoMapper.selectList();
        if (CollUtil.isNotEmpty(sysUserInfos)) {
            Object obj = new Object();
            sysUserInfos.parallelStream().forEach(userInfo -> {
                synchronized (obj) {
                    Map<String, Object> userInfoMap = ConvertUtil.convertToMap(userInfo);
                    userInfoMap.put("updateTime", DateUtilX.format(userInfo.getUpdateTime(), DateConstant.YYYY_MM_DD_HH_MM_SS_1));
                    userInfoMap.put("createTime", DateUtilX.format(userInfo.getCreateTime(), DateConstant.YYYY_MM_DD_HH_MM_SS_1));
                    RedisHelper.hmSet(TokenConstant.USER_INFO + userInfo.getId(), userInfoMap);
                }
            });
        }
    }

    /**
     * 缓存短信模版信息
     */
    @Override
    public void cacheSmsTemplate() {
        String enabled = smsProperties.getEnabled();
        Optional.ofNullable(SmsServerEnum.getByInfo(enabled))
                .orElseThrow(() -> new CommonException("There is no corresponding SMS platform information！"));
        List<SmsTemplate> smsTemplates = smsTemplateMapper.selectList(new LambdaQueryWrapper<SmsTemplate>()
                .eq(SmsTemplate::getServer, enabled));
        if (CollUtil.isNotEmpty(smsTemplates)) {
            smsTemplates.stream().forEach(smsTemplate -> {
                RedisHelper.hSet(SmsConstant.SMS_TEMPLATE + smsTemplate.getType(), SmsConstant.SMS_TEMPLATE_ID, smsTemplate.getTemplateId());
                RedisHelper.hSet(SmsConstant.SMS_TEMPLATE + smsTemplate.getType(), SmsConstant.SMS_TEMPLATE_DESC, smsTemplate.getTempDesc());
            });
        }
    }

}
